package com.mdhw.system.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.RandomUtil;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mdhw.common.contants.CacheKeyContants;
import com.mdhw.common.enums.StatusEnum;
import com.mdhw.config.R;
import com.mdhw.mili.vo.area.RegionVo;
import com.mdhw.system.dao.SysRegionDao;
import com.mdhw.system.entity.SysRegion;
import com.mdhw.system.service.SysRegionService;
import com.mdhw.system.service.SysUserService;
import com.mdhw.system.vo.*;
import com.mdhw.utils.AliYunOssServer;
import com.mdhw.utils.DateUtil;
import com.mdhw.utils.RedisCompanyUtil;
import com.mdhw.utils.RedisUtil;
import com.mdhw.utils.excel.ExportExcelUtils;
import com.mdhw.utils.excel.ImportExcelUtil;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.util.IOUtils;
import org.ini4j.Reg;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.io.OutputStream;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@Service
public class SysRegionServiceImpl extends ServiceImpl<SysRegionDao, SysRegion> implements SysRegionService {

    @Autowired
    private SysUserService sysUserService;
    @Autowired
    private AliYunOssServer aliYunOssServer;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private RedisCompanyUtil redisCompanyUtil;

    @Override
    public R<List<RegionVo>> selectRegionTree() {

        List<RegionVo> sysRegionTreeList = new ArrayList<>();


        LambdaQueryWrapper<SysRegion> srLqw = new LambdaQueryWrapper<>();
        srLqw.eq(SysRegion::getStatus, StatusEnum.OK.getCode());
        //srLqw.lt(SysRegion::getType,3);
        List<SysRegion> sysRegionList = this.list(srLqw);
        List<RegionVo> parentSysRegionTreeList = new ArrayList<>();
        for(SysRegion sysRegion:sysRegionList){

            Long pid = sysRegion.getPid();
            RegionVo sysRegionTree = new RegionVo();
            BeanUtils.copyProperties(sysRegion,sysRegionTree);

            String name = sysRegionTree.getName();
            sysRegionTree.setText(name);
            sysRegionTree.setValue(sysRegion.getCode());


            sysRegionTreeList.add(sysRegionTree);
            if(pid == 0){
                parentSysRegionTreeList.add(sysRegionTree);
            }
        }
        for(RegionVo sysRegionTree:parentSysRegionTreeList){
            List<RegionVo> child = getChild(sysRegionTree.getId(), sysRegionTreeList);

            if(child != null && child.size() > 0){
                sysRegionTree.setChildren(child);
            }
        }
        long end = System.currentTimeMillis();
        System.out.println();
        return R.success(parentSysRegionTreeList);
    }




    private List<RegionVo> getChild(Long parentId, List<RegionVo> sysRegionTreeList){
        // 子菜单列表
        List<RegionVo> childList = new ArrayList<>();
        for(RegionVo sysMenuTree:sysRegionTreeList){
            if(sysMenuTree.getPid().toString().equals(parentId.toString())){
                childList.add(sysMenuTree);
            }
        }

        // 遍历 获取子菜单的子菜单
        for (RegionVo menu : childList) {
            List<RegionVo> child = getChild(menu.getId(), sysRegionTreeList);
            if(child != null && child.size() > 0){
                menu.setChildren(child);
            }
        }
        //递归出口 childList长度为0
        if (childList.size() == 0) {
            return new ArrayList<>();
        }
        return childList;
    }





}
